package com.ibm.workplace.wcm.cqplugin.ui.actions;

import java.util.*;

import org.eclipse.jface.viewers.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.actions.*;

import com.ibm.workplace.wcm.cqplugin.*;

public class ViewDefectAction extends SelectionProviderAction {

    public ViewDefectAction(ISelectionProvider provider) {
        super(provider, "View...");

        setImageDescriptor(Activator.getDefault().getImageRegistry().getDescriptor(Activator.IMAGE_BUG));
    }

    public void selectionChanged(IStructuredSelection selection) {
        super.selectionChanged(selection);

        boolean enabled = false;

        if (selection.size() >= 1 && selection.getFirstElement() instanceof IBugInfo) {
            enabled = true;
        }

        setEnabled(enabled);
    }

    public void run() {
        Shell shell = null;
        if (getSelectionProvider() instanceof TableViewer) {
            shell = ((TableViewer)getSelectionProvider()).getTable().getShell();
        } else {
            shell = Display.getDefault().getActiveShell();
        }
        
        IStructuredSelection ss = (IStructuredSelection)getSelection();
        IBugInfo[] bugs = new IBugInfo[ss.size()];
        int index = 0;
        for (Iterator it = ss.iterator(); it.hasNext(); index++) {
            Object o = it.next();
            bugs[index] = (IBugInfo)o;           
        }
        
        if (bugs.length > 0) {
        	if(Activator.DEBUG_ON)
        		System.out.println("BugInfoPluginActivator.getDefault()");
        	Activator actvtr = Activator.getDefault();
        	if(Activator.DEBUG_ON)
        		System.out.println("actvtr.showBugs");
        	actvtr.showBugs(shell, bugs);
        }    
    }

}
